<script setup lang="ts">
import { onMounted, ref, onBeforeUnmount } from 'vue'
import * as echarts from 'echarts/core'
import {
  TitleComponent,
  TooltipComponent,
  LegendComponent,
} from 'echarts/components'
import { PieChart } from 'echarts/charts'
import { LabelLayout } from 'echarts/features'
import { CanvasRenderer } from 'echarts/renderers'

echarts.use([
  TitleComponent,
  TooltipComponent,
  LegendComponent,
  PieChart,
  CanvasRenderer,
  LabelLayout,
])
const chartDom = ref<HTMLElement | null>(null)
let myChart: echarts.ECharts | null = null
onMounted(() => {
  if (chartDom.value) {
    myChart = echarts.init(chartDom.value)
    myChart.setOption({
      title: {
        text: '本公司成员分布',
        subtext: '成员分布',
        left: 'center',
      },
      tooltip: {
        trigger: 'item',
      },
      legend: {
        orient: 'vertical',
        left: 'left',
      },
      series: [
        {
          name: '人数信息',
          type: 'pie',
          radius: '50%',
          data: [
            { value: 1048, name: '用户' },
            { value: 735, name: '产品管理员' },
            { value: 580, name: '用户管理员' },
            { value: 484, name: '消息管理员' },
          ],
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)',
            },
          },
        },
      ],
    })
  }
})
// 在组件卸载时清理图表实例，防止内存泄漏
onBeforeUnmount(() => {
  if (myChart) {
    myChart.dispose()
    myChart = null
  }
})
</script>

<template>
  <div class="echart" ref="chartDom"></div>
</template>

<style scoped lang="scss">
.echart {
  height: 380px;
  padding: 15px;
  overflow: hidden;
}
</style>
